package com.znzz.course.util.db;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import com.znzz.course.activity.MainActivity;
import com.znzz.course.entity.Course;

import java.util.ArrayList;

/**
 * 课程数据库操作方法
 * @Author by DHF
 * @Date 2022/04/2022/4/18 10:20
 * @Version 1.0
 */
public class CourseDao {
    private Context context;

    public CourseDao(Context context) {
        this.context = context;
    }

    //保存课程信息数据到本地数据库
    public void saveData(Course course) {
        SQLiteDatabase sqLiteDatabase =  DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL
                ("insert into courses(course_name, teacher, class_room, day, class_start, class_end,week_start," +
                                "week_end) " +
                                "values(?, ?, ?, ?, ?, ?,?,?)",
                        new String[] {course.getCourseName(),
                                course.getTeacher(),
                                course.getClassRoom(),
                                course.getDay()+"",
                                course.getClassStart()+"",
                                course.getClassEnd()+"",
                                course.getWeekStart()+"",
                                String.valueOf(course.getWeekEnd())}
                );
        sqLiteDatabase.close();
    }
    //添加/修改备注
    public void courseRemark(String remark_information,String className){
        SQLiteDatabase sqLiteDatabase =  DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL
                ("update courses set remark='"+remark_information+"'where course_name ='"+className+"'");
        sqLiteDatabase.close();
    }

    //从数据库加载数据形成列表
    @SuppressLint("Range")
    public ArrayList<Course> loadData() {
        ArrayList<Course> coursesList = new ArrayList<>(); //课程列表
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        Cursor cursor = sqLiteDatabase.rawQuery("select * from courses", null);
        if (cursor.moveToFirst()) {
            do {
                coursesList.add(new Course(
                        cursor.getString(cursor.getColumnIndex("course_name")),
                        cursor.getString(cursor.getColumnIndex("teacher")),
                        cursor.getString(cursor.getColumnIndex("class_room")),
                        cursor.getInt(cursor.getColumnIndex("day")),
                        cursor.getInt(cursor.getColumnIndex("class_start")),
                        cursor.getInt(cursor.getColumnIndex("class_end")),
                        cursor.getInt(cursor.getColumnIndex("week_start")),
                        cursor.getInt(cursor.getColumnIndex("week_end")),
                        cursor.getString(cursor.getColumnIndex("remark"))));
            } while(cursor.moveToNext());
        }
        cursor.close();
        return coursesList;
    }

    /**
     * 删除课程信息
     * @param course
     */
    public void deleteCourse(Course course){
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL("delete from courses where course_name = ?", new String[]{course.getCourseName()});
        sqLiteDatabase.close();
    }
    /**
     * 删除所有课程信息
     */
    public void deleteAllCourse(){
        SQLiteDatabase sqLiteDatabase = DBManager.getSqliteWritableDatabase(context);
        sqLiteDatabase.execSQL("delete from courses");
        sqLiteDatabase.close();
    }

}
